package action.user;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

import db.*;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements Preparable{
	private List<Customer> customerList;
	private List<RealEstate> realestateList;
	private List<ServiceContract> servicecontractList;
	private List<db.Transaction> transactionList;
	private List<db.File> fileList;
	private List<Bid> bidList;
	private RealEstate realestate;
	private Customer cust;
	private Set<RealEstate> realestateSet;
	private List<String> sc_types;
	private List<String> bd_types;
	private List<String> statuses;
	private String newSC_reId;
	private String newTR_scId;
	private String newBD_trId;
	private ServiceContract servicecontract;
	private db.Transaction transaction;
	private Bid bid;
	private String file_scId;
	private String file_trId;
	private String file_BId;
	private String backLink;
	
	public void prepare() {
		sc_types = new ArrayList<String>();
		sc_types.add("Seller's");
		sc_types.add("Buyer's");
		bd_types = new ArrayList<String>();
		bd_types.add("Seller's");
		bd_types.add("Buyer's");
    }
	
	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

	public List<RealEstate> getRealestateList() {
		return realestateList;
	}

	public void setRealestateList(List<RealEstate> realestateList) {
		this.realestateList = realestateList;
	}
	
	public List<ServiceContract> getServicecontractList() {
		return servicecontractList;
	}

	public void setServicecontractList(List<ServiceContract> servicecontractList) {
		this.servicecontractList = servicecontractList;
	}

	public List<db.Transaction> getTransactionList() {
		return transactionList;
	}

	public void setTransactionList(List<db.Transaction> transactionList) {
		this.transactionList = transactionList;
	}

	public List<db.File> getFileList() {
		return fileList;
	}

	public void setFileList(List<db.File> fileList) {
		this.fileList = fileList;
	}

	public List<Bid> getBidList() {
		return bidList;
	}

	public void setBidList(List<Bid> bidList) {
		this.bidList = bidList;
	}

	public RealEstate getRealestate() {
		return realestate;
	}

	public void setRealestate(RealEstate realestate) {
		this.realestate = realestate;
	}

	public Customer getCust() {
		return cust;
	}

	public void setCust(Customer cust) {
		this.cust = cust;
	}

	public Set<RealEstate> getRealestateSet() {
		return realestateSet;
	}

	public void setRealestateSet(Set<RealEstate> realestateSet) {
		this.realestateSet = realestateSet;
	}
	
	public List<String> getSc_types() {
		return sc_types;
	}

	public void setSc_types(List<String> scTypes) {
		sc_types = scTypes;
	}
	
	public List<String> getBd_types() {
		return bd_types;
	}

	public void setBd_types(List<String> bdTypes) {
		bd_types = bdTypes;
	}

	public List<String> getStatuses() {
		return statuses;
	}

	public void setStatuses(List<String> statuses) {
		this.statuses = statuses;
	}

	public String getNewSC_reId() {
		return newSC_reId;
	}

	public void setNewSC_reId(String newSCReId) {
		newSC_reId = newSCReId;
	}

	public String getNewTR_scId() {
		return newTR_scId;
	}

	public void setNewTR_scId(String newTRScId) {
		newTR_scId = newTRScId;
	}

	public String getNewBD_trId() {
		return newBD_trId;
	}

	public void setNewBD_trId(String newBDTrId) {
		newBD_trId = newBDTrId;
	}

	public ServiceContract getServicecontract() {
		return servicecontract;
	}

	public void setServicecontract(ServiceContract servicecontract) {
		this.servicecontract = servicecontract;
	}

	public db.Transaction getTransaction() {
		return transaction;
	}

	public void setTransaction(db.Transaction transaction) {
		this.transaction = transaction;
	}

	public Bid getBid() {
		return bid;
	}

	public void setBid(Bid bid) {
		this.bid = bid;
	}

	public String getFile_scId() {
		return file_scId;
	}

	public void setFile_scId(String fileScId) {
		file_scId = fileScId;
	}

	public String getFile_trId() {
		return file_trId;
	}

	public void setFile_trId(String fileTrId) {
		file_trId = fileTrId;
	}

	public String getFile_BId() {
		return file_BId;
	}

	public void setFile_BId(String fileBId) {
		file_BId = fileBId;
	}

	public String getBackLink() {
		return backLink;
	}

	public void setBackLink(String backLink) {
		this.backLink = backLink;
	}

	@SuppressWarnings("unchecked")
	public String show_the_SCs(){
		Map session = ActionContext.getContext().getSession();
		Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		HttpServletRequest request = ServletActionContext.getRequest();
		String temp_reId = request.getParameter("reId");
		Integer int_temp_reId =-1;
		if(temp_reId != null)
			int_temp_reId = Integer.parseInt(temp_reId);
		ServiceContractDAO scDao = new ServiceContractDAO();
		try{
			servicecontractList = scDao.findByUId(uId);//uId should be of type - Integer !!
			if ( !int_temp_reId.equals(-1)){
				//System.out.println("entered: uId: "+uId+" reId: "+temp_reId);
				List<ServiceContract> filteredSCList = new ArrayList();
				Iterator scIterator =servicecontractList.iterator();
				while(scIterator.hasNext()){
					ServiceContract currentSC = (ServiceContract)(scIterator.next());
					boolean matchRE = currentSC.getReId().equals(int_temp_reId);
					if (matchRE)
						filteredSCList.add(currentSC);
				}
				servicecontractList = filteredSCList;
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_the_TRs(){
		Map session = ActionContext.getContext().getSession();
		String userRole = (String)session.get("userRole");
		Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		HttpServletRequest request = ServletActionContext.getRequest();
		String temp_scId = request.getParameter("scId");
		Integer int_temp_scId =-1;
		if(temp_scId != null)
			int_temp_scId = Integer.parseInt(temp_scId);
		ServiceContractDAO scDao = new ServiceContractDAO();
		ServiceContract temp_sc = scDao.findById(int_temp_scId) ;
		if (temp_sc == null )
			return ERROR;
		if(!temp_sc.getUId().equals(uId)){
			if(userRole.equals("0")){
				System.out.println("Wrong privilege!!");
				return ERROR;
			}
		}
		Set transactionSet = temp_sc.getTransactions();
		transactionList = new ArrayList(transactionSet);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_the_BDs(){
		Map session = ActionContext.getContext().getSession();
		String userRole = (String)session.get("userRole");
		Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		HttpServletRequest request = ServletActionContext.getRequest();
		String temp_trId = request.getParameter("trId");
		Integer int_temp_trId =-1;
		if(temp_trId != null)
			int_temp_trId = Integer.parseInt(temp_trId);
		TransactionDAO trDao = new TransactionDAO();
		db.Transaction temp_tr = trDao.findById(int_temp_trId) ;
		if (temp_tr == null )
			return ERROR;
		if(!temp_tr.getServiceContract().getUId().equals(uId)){
			if(userRole.equals("0")){
				System.out.println("Wrong privilege!!");
				return ERROR;
			}
		}
		Set bidSet = temp_tr.getBids();
		if(bidSet == null)
			bidList = new ArrayList();
		else bidList = new ArrayList(bidSet);
		/*Iterator a = bidList.iterator();
		while(a.hasNext()){
			System.out.println(((Bid)a.next()).getBDescription());
		}*/
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_the_files(){
		Map session = ActionContext.getContext().getSession();
		String userRole = (String)session.get("userRole");
		Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		HttpServletRequest request = ServletActionContext.getRequest();
		String temp_scId = request.getParameter("scId");
		String temp_trId = request.getParameter("trId");
		String temp_Bid = request.getParameter("BId");
		if(temp_scId ==null||temp_trId==null||temp_Bid==null)
			return ERROR;
		FileDAO fileDao = new FileDAO();
		Integer int_temp_scId =-1;
		Integer int_temp_trId =-1;
		Integer int_temp_Bid =-1;
		int_temp_scId = Integer.parseInt(temp_scId);
		int_temp_trId = Integer.parseInt(temp_trId);
		int_temp_Bid = Integer.parseInt(temp_Bid);
		ServiceContractDAO scDao = new ServiceContractDAO();
		ServiceContract temp_sc = scDao.findById(int_temp_scId) ;
		if (temp_sc == null )
			return ERROR;
		if(!temp_sc.getUId().equals(uId)){
			if(userRole.equals("0")){
				System.out.println("Wrong privilege!!");
				return ERROR;
			}
		}			
		if(int_temp_trId<=0 && int_temp_Bid <=0 ){//sc
			Set fileSet = temp_sc.getFiles();		
			fileList = new ArrayList(fileSet);
			request.setAttribute("link", "sc");
			request.setAttribute("no.", ""+int_temp_scId);
		}
		else if(int_temp_trId>0 && int_temp_Bid <=0){//tr
			fileList = fileDao.findByTrId(int_temp_trId);
			request.setAttribute("link", "tr");
			request.setAttribute("no.", ""+int_temp_trId);
		}else{//bd
			fileList = fileDao.findByBId(int_temp_Bid);
			request.setAttribute("link", "bd");
			request.setAttribute("no.", ""+int_temp_Bid);
		}
		/*Iterator a = fileList.iterator();
		while(a.hasNext()){
			System.out.println(((db.File)a.next()).getFDescription());
		}*/
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_all_customers(){
		CustomerDAO customerDao = new CustomerDAO();
		customerList = customerDao.findAll();		
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_all_real_estates(){
		RealEstateDAO reDao = new RealEstateDAO();
		realestateList = reDao.findAll();
		return SUCCESS;
	}
	
	public String details_real_estate(){
		RealEstateDAO reDao = new RealEstateDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("reId"));
		realestate = reDao.findById(id);
		return SUCCESS;
	}
	
	public String details_customer(){
		CustomerDAO custDao = new CustomerDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("custId"));
		cust = custDao.findById(id);		
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String show_cust_REs(){
		CustomerDAO custDao = new CustomerDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("custId"));
		Customer custTmp = custDao.findById(id);
		realestateSet = custTmp.getRealEstates(); 
		return SUCCESS;
	}
	
	public String go_add_service_contract(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.setNewSC_reId(request.getParameter("reId"));
		return SUCCESS;
	}
	
	public String go_add_transaction(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.setNewTR_scId(request.getParameter("scId"));
		return SUCCESS;
	}
	
	public String go_add_bid(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.setNewBD_trId(request.getParameter("trId"));
		return SUCCESS;
	}
	
	public String go_add_file(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.setFile_scId(request.getParameter("scId"));
		this.setFile_trId(request.getParameter("trId"));
		this.setFile_BId(request.getParameter("BId"));

		Integer int_temp_scId =-1;
		Integer int_temp_trId =-1;
		Integer int_temp_Bid =-1;
		int_temp_scId = Integer.parseInt(file_scId);
		int_temp_trId = Integer.parseInt(file_trId);
		int_temp_Bid = Integer.parseInt(file_BId);
				
		if(int_temp_trId<=0 && int_temp_Bid <=0 ){//sc			
			request.setAttribute("link", "sc");
			request.setAttribute("no.", ""+int_temp_scId);
		}
		else if(int_temp_trId>0 && int_temp_Bid <=0){//tr			
			request.setAttribute("link", "tr");
			request.setAttribute("no.", ""+int_temp_trId);
		}else{//bd			
			request.setAttribute("link", "bd");
			request.setAttribute("no.", ""+int_temp_Bid);
		}
		
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String details_service_contract(){
		Map session = ActionContext.getContext().getSession();
		//Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		String userId_session = (String)session.get("userId");
		ServiceContractDAO scDao = new ServiceContractDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int sc_id = -1;
		try{
			sc_id = Integer.parseInt(request.getParameter("scId"));
		}catch(Exception e){
			return ERROR;
		}
		servicecontract = scDao.findById(sc_id);
		String tmp_uid = servicecontract.getUId().toString();
		if (!tmp_uid.equals(userId_session)){
			System.out.println("No privilege!!");
			return ERROR;
		}
		statuses = new ArrayList<String>();
		statuses.add("Ongoing");
		statuses.add("Ended");
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String details_transaction(){
		Map session = ActionContext.getContext().getSession();
		//Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		String userId_session = (String)session.get("userId");
		TransactionDAO trDao = new TransactionDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int tr_id = -1;
		try{ 
			tr_id =Integer.parseInt(request.getParameter("trId"));
		}catch(Exception e){return ERROR;}
		transaction = trDao.findById(tr_id);
		String tmp_uid = transaction.getServiceContract().getUId().toString();
		if (!tmp_uid.equals(userId_session)){
			System.out.println("No privilege!!");
			return ERROR;
		}
		statuses = new ArrayList<String>();
		statuses.add("Ongoing");
		statuses.add("Ended");
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String details_bid(){
		Map session = ActionContext.getContext().getSession();
		//Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		String userId_session = (String)session.get("userId");
		BidDAO bdDao = new BidDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int bd_id = Integer.parseInt(request.getParameter("bdId"));
		if(bd_id == 0)return ERROR;
		bid = bdDao.findById(bd_id);
		String tmp_uid = bid.getTransaction().getServiceContract().getUId().toString();
		if (!tmp_uid.equals(userId_session)){
			System.out.println("No privilege!!");
			return ERROR;
		}
		if (bid.getBType()==0)
			request.setAttribute("bidtype", "Buyer's Bid");
		else 
			request.setAttribute("bidtype", "Seller's Feedback");
		return SUCCESS;
	}
	
}
